package cn.mesmile.order.controller;

import cn.mesmile.lock.aop.LockAction;
import cn.mesmile.order.entity.Order;
import cn.mesmile.order.fallback.ProductServiceClient;
import cn.mesmile.order.service.OrderService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zb
 * @date 2020/12/16 14:56
 * @Description
 */
@AllArgsConstructor
@RequestMapping("/api/v1/order")
@RestController
public class OrderController {

    private final ProductServiceClient productServiceClient;

    private final OrderService orderService;

    @GetMapping("/getProduct")
    public Order get(){
        Order order = new Order();
        order.setOrderName("【订单中获取到结果】"+ productServiceClient.get());
        order.setId(1L);
        order.setUserId(1000L);
        return order ;
    }

    @LockAction(value = "orderLock")
    @GetMapping("/create/{productId}/{number}")
    public Object create(@PathVariable("productId")Long productId, @PathVariable("number")Long number){
        Boolean transaction = false;
        try {
            transaction = orderService.transaction(productId, number);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return transaction;
    }


}
